package com.boc.langxiang.presenter.me;

import com.boc.langxiang.contract.me.MyCollectionsActivityContract;
import com.boc.langxiang.entity.request.DelMycollectRequest;
import com.boc.langxiang.entity.request.MyCollectionRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.CollectEntity;
import com.boc.langxiang.entity.response.MyCollectionContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/27.
 */
public class MyCollectionsActivityPresenter implements MyCollectionsActivityContract.Presenter {

    private ApiService mService;
    MyCollectionsActivityContract.View mView;

    public MyCollectionsActivityPresenter(MyCollectionsActivityContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;


    }

    @Override
    public void myCollection(int page) {
        mView.showLoading();
        MyCollectionRequest request = new MyCollectionRequest();
        request.page = page;
        mService.myCollection(request.params())
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<MyCollectionContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<MyCollectionContent> response) {
                        MyCollectionContent content = response.getContent();
                        if (content != null) {
                            List<CollectEntity> collect = content.getCollect();
                            mView.myCollectionSuccess(collect);
                        } else {
                            mView.myCollectionSuccess(null);
                        }
                    }
                });
    }

    @Override
    public void delMycollect(String id, final int position) {
        mView.showDelLoading();
        DelMycollectRequest request = new DelMycollectRequest();
        request.id = id;
        mService.delMycollect(request.params())
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.delSuccess(position);
                    }
                });
    }
}
